package com.doradance.common.web;

import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;

import com.pachong.android.framework.httprequest.responsecallbacks.DataRequestListener;
import com.pachong.android.frameworkbase.utils.EasyToast;


import org.greenrobot.eventbus.EventBus;

import static android.app.Activity.RESULT_OK;

/**
 * Created by Administrator on 2017/8/30.
 */

public class GetMediaAndUploadFragment extends Fragment {
    public static final String TAG = GetMediaAndUploadFragment.class.getSimpleName();
    public static final int REQ_CODE = 1000;

    public static final int MEDIA_TYPE_AUDIO = 0;
    public static final int MEDIA_TYPE_VIDEO = 1;
    public static final int MEDIA_TYPE_PIC = 2;


    private int requestMedia = MEDIA_TYPE_VIDEO;
    ProgressDialog dialog;
    public GetMediaAndUploadFragment() {
        // Required empty public constructor

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dialog = new ProgressDialog(getContext());
    }

    public static GetMediaAndUploadFragment newInstance() {
        GetMediaAndUploadFragment fragment = new GetMediaAndUploadFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    public void getVedio() {
        Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, REQ_CODE);
        requestMedia = MEDIA_TYPE_VIDEO;
    }
    public void getAudio() {
        Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, REQ_CODE);
        requestMedia = MEDIA_TYPE_AUDIO;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Log.e(TAG, "onActivityResult: ");
        if (requestCode == REQ_CODE && resultCode == RESULT_OK && null != data) {
            Uri selectedMedia = data.getData();
            String[] filePathColumn = new String[1];
            if (requestMedia == MEDIA_TYPE_VIDEO) {
                filePathColumn[0] = MediaStore.Video.Media.DATA;
            } else if (requestMedia == MEDIA_TYPE_AUDIO) {
                filePathColumn[0] = MediaStore.Audio.Media.DATA;
            } else {
                return;
            }

            Cursor cursor = getActivity().getContentResolver().query(selectedMedia,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            Log.e(TAG, "onActivityResult: media file path " + filePath);

            Uri uri = Uri.parse(filePath);
            cursor.close();
        }
    }
    }
